<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="40%">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="150"
    >
      <el-form-item label="申请退款金额(元)" prop="transAmount">
        <el-input v-model="formData.transAmount" :maxlength="10" placeholder="请输入申请退款金额" />
      </el-form-item>
      <el-form-item label="付款凭证" prop="transVoucher">
        <UploadImg :limit="10" v-model="formData.transVoucher" />
      </el-form-item>
      <el-form-item label="支付密码" prop="payPassword">
        <el-input
          type="password"
          :maxlength="16"
          show-password
          v-model="formData.payPassword"
          placeholder="请输入支付密码"
          autocomplete="new-password"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="formData.remark" :maxlength="100" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { validateTransAmount } from '@/utils/validatorUtil'
import * as financial from '@/api/financial'
import { cloneDeep } from 'lodash'
const emit = defineEmits(['success'])
const formRef = ref()
const { t } = useI18n()
const message = useMessage()
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formData: Record<string, any> = ref({
  transAmount: undefined,
  transVoucher: undefined,
  payPassword: undefined,
  remark: undefined,
  approvalType: 'deductMoney'
})
const rowItem = ref()

const formRules = reactive({
  transAmount: [
    { required: true, message: '充值金额不能为空', trigger: 'blur' },
    { validator: validateTransAmount, trigger: 'blur' }
  ],
  transVoucher: [{ required: true, message: '付款凭证不能为空', trigger: 'change' }],
  payPassword: [
    { required: true, message: '支付密码不能为空', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度6-16位', trigger: 'blur' }
  ]
})

const open = async (data: Record<string, any>) => {
  resetFn()
  dialogVisible.value = true
  dialogTitle.value = '申请退款'
  rowItem.value = data
}

const submitForm = async () => {
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  try {
    formLoading.value = true
    const data = cloneDeep(rowItem.value)
    delete data.createTime
    await financial.financeDeductCreate({ ...formData.value, ...data })
    message.success(t('common.operateSuccess'))
    dialogVisible.value = false
    resetFn()
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const resetFn = () => {
  formData.value = {
    transAmount: undefined,
    transVoucher: undefined,
    payPassword: undefined,
    remark: undefined,
    approvalType: 'deductMoney'
  }
  formRef.value?.resetFields()
}

defineExpose({ open })
</script>
